﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using CE.Database;
using CE.Security;

/// <summary>
/// Summary description for ApplicationDB
/// </summary>
namespace CE.Job
{
    public class ApplicationDB
    {
        public ApplicationDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public Int64 InsertUpdate(Application application)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "sprocApplicationInsertUpdateSingleItem";
            SortedList _sPara = new SortedList();
            #region Parameter
            _sPara.Add("@ApplicationID", application.ApplicationID);
            _sPara.Add("@Comments", application.Comments);
            _sPara.Add("@RefUserID", application.RefUserID);
            _sPara.Add("@RefJobID", application.RefJobID);
            _sPara.Add("@ApplicationStatus", application.ApplicationStatus);
            _sPara.Add("@Remark", application.Remark);
            _sPara.Add("@VerifiedBy", application.VerifiedBy);
            _sPara.Add("@Info1", application.Info1);
            _sPara.Add("@Info2", application.Info2);
            _sPara.Add("@Info3", application.Info3);
            _sPara.Add("@Info4", application.Info4);
            _sPara.Add("@Info5", application.Info5);
            _sPara.Add("@Info6", application.Info6);
            _sPara.Add("@Info7", application.Info7);
            _sPara.Add("@Info8", application.Info8);
            _sPara.Add("@Info9", application.Info9);
            _sPara.Add("@Info10", application.Info10);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);
            return _result;
        }
        public Application GetApplicationByID(int ApplicationID)
        {
            Application _application = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocApplicationSelectSingleItemByID]";

            SortedList _spara = new SortedList();
            _spara.Add("@ApplicationID", ApplicationID);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _application = new Application();
                    _application = FillDataRecord(_dt);
                }
            }
            return _application;
        }
        private Application FillDataRecord(DataTable myDataTable)
        {
            Application myApplication = new Application();
            myApplication.ApplicationID = Convert.ToInt64(myDataTable.Rows[0]["ApplicationID"].ToString());
            myApplication.Comments = myDataTable.Rows[0]["Comments"].ToString();
            myApplication.RefUserID = Convert.ToInt64(myDataTable.Rows[0]["RefUserID"].ToString());
            myApplication.RefJobID = Convert.ToInt64(myDataTable.Rows[0]["RefJobID"].ToString());
            myApplication.ApplicationStatus = myDataTable.Rows[0]["ApplicationStatus"].ToString();
            myApplication.Remark = myDataTable.Rows[0]["Remark"].ToString();
            myApplication.VerifiedBy = Convert.ToInt64(myDataTable.Rows[0]["VerifiedBy"].ToString());
            myApplication.CreatedDate = Convert.ToDateTime(myDataTable.Rows[0]["CreatedDate"].ToString());
            myApplication.ModifiedDate = Convert.ToDateTime(myDataTable.Rows[0]["ModifiedDate"].ToString());

            return myApplication;
        }

        private Application FillDataRecord(IDataRecord myDataRecord)
        {
            Application myApplication = new Application();
            myApplication.ApplicationID = myDataRecord.GetInt64(myDataRecord.GetOrdinal("ApplicationID"));
            myApplication.Comments = myDataRecord.GetString(myDataRecord.GetOrdinal("Comments"));
            myApplication.RefUserID = myDataRecord.GetInt64(myDataRecord.GetOrdinal("RefUserID"));
            myApplication.RefJobID = myDataRecord.GetInt64(myDataRecord.GetOrdinal("RefJobID"));
            myApplication.ApplicationStatus = myDataRecord.GetString(myDataRecord.GetOrdinal("ApplicationStatus"));
            myApplication.Remark = myDataRecord.GetString(myDataRecord.GetOrdinal("Remark"));
            myApplication.VerifiedBy = myDataRecord.GetInt64(myDataRecord.GetOrdinal("VerifiedBy"));
            myApplication.CreatedDate = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDate"));
            myApplication.ModifiedDate = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDate"));
            return myApplication;
        }
    }
}